/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2511.java
 * @Author : WangRuoyu
 * @Date 2023/1/11 9:58
 */

package leetcode;

public class Q2511 {
    public int captureForts(int[] forts) {
        int res = 0;

        int tmp;
        int index;
        for (int i = 0; i < forts.length; i++) {
            if (forts[i] == 1 || forts[i] == -1) {
                tmp = forts[i];
                index = i;
                i++;
                while ((i < forts.length) && (forts[i] == 0)) {
                    i++;
                }
                if (i == forts.length) {
                    break;
                } else {
                    if (forts[i] == (-tmp)) {
                        res = Math.max(res, i - index - 1);
                    }
                    i--;
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Q2511 sl = new Q2511();
        int[] forts = {1, 0, 0, -1, 0, 0, 0, 0, 1};
        System.out.println(sl.captureForts(forts));
    }
}
